
#include "websocket_common.h"

#include <stdio.h>
#include <stdlib.h> //exit()
#include <string.h>
#include <errno.h>
#include <unistd.h> //getpid
#include <pthread.h>

char ip[128] = {0};//"172.16.23.160";// 本机IP
int port = 9753;
typedef struct{
    int fd;
    WebsocketData_Type datatype;
    char *buf;
}pthreadArg;
void* thread_fun(void *arge)
{
    pthreadArg *pArg=(pthreadArg*)arge;
    printf("消息：%s\r\n",pArg->buf);
    if(pArg->buf) free(pArg->buf);
    if(pArg) free(pArg);
    pthread_exit(0);
}
/******************************************
 * 响应客户端
 * 参数： fd 句柄
 *       datatype 数据类型
 *       buf 缓存
 *       bufLen 缓存大小
 * 说明：数据接收数据后，请在此对数据进行处理。
******************************************/
int client_callBack(int fd,WebsocketData_Type datatype, char *buf, unsigned int bufLen)
{
    int ret=0;
    if(bufLen > 0)
	{
        /*
		printf("client(fd/%d) recv:\r\n"
            "数据类型：%d\r\n"
            "数据长度byte:%d\r\n"
            "内容：\r\n%s\r\n"
            , fd,datatype,bufLen, buf);		*/

           // printf("消息：%s\r\n",buf);
		char *pthreadBuf=(char*)calloc(bufLen,sizeof(char));
        memcpy(pthreadBuf,buf,bufLen);
        pthreadArg *pArg=(pthreadArg*)calloc(1,sizeof(pthreadArg));
        pArg->fd=fd;
        pArg->datatype=datatype;
        pArg->buf=pthreadBuf;
        pthread_t thread_id;
        if(pthread_create(&thread_id, NULL, thread_fun, (void *)(pArg)) != 0)
	    {
		    printf("创建处理线程失败!\r\n");
		    return -1;
	    } 
	}

	return ret;
}
int client_callCloseed(int fd)
{
    printf("服务器已关闭！！！！%d\r\n",fd);    

    //pthread_cancel(client_thread_id);     // 等待线程关闭

    return 0;
}
int main(int argc,char *argv[])
{
    int ret, timeCount = 0;
    int fd;
    char buff[1024];
    int pid;
    unsigned long int cid;
 
    if(argc<2)
    {
        printf("未输入服务器地址，自动使用127.0.0.1。\r\n");
        sprintf(ip,"127.0.0.1");
    }
    else
        sprintf(ip,"%s",argv[1]);
    //
    pid = getpid();
    printf("\r\n========== client(%d) start ! ==========\r\n\r\n", pid);
    printf("\r\n输入：\r\n");
        printf("\thelp;\t显示帮助。\r\n");
        printf("\tjoin:昵称\t以当前名称登入。\r\n");
        printf("\ttoall;内容\t向所有在线用户发送。\r\n");
        printf("\tto:昵称;内容\t特定发送。\r\n");
        printf("\tgetusers;\t获取在线用户。\r\n");
        printf("\texit;\t退出。\r\n");
    //
    //netCheck_getIP("wlp3s0", ip);
    
    if((fd=client_init(ip,port,"/null",(CallBackFun*)&client_callBack,(CallCloseedFun*)&client_callCloseed)) <= 0)
    {
        printf("client_init failed !\r\n");
        return -1;
    }   

    
    webSocket_delayms(100);
    memset(buff, 0, sizeof(buff));
    sprintf(buff, "Say hi~ from client(%d)", pid);
    webSocket_send(fd, buff, strlen(buff), true, WDT_TXTDATA);
    
    //登入
    if(argc>2)
    {
        memset(buff,0,sizeof(buff));
        sprintf(buff,"join:%s",argv[2]);
        webSocket_send(fd, buff, strlen(buff), true, WDT_TXTDATA);
    }
    //
 
    while(1)
    {     
        gets(buff);
        if(strlen(buff)>0){
        if(strstr(buff,"exit;")==buff)
        {
            webSocket_send(fd,buff,strlen(buff),true,WDT_DISCONN);
            close(fd);
            break;
        }
        else if(strstr(buff,"help;")==buff)
        {
        printf("\r\n输入：\r\n");
        printf("\thelp;\t显示帮助。\r\n");
        printf("\tjoin:昵称\t以当前名称登入。\r\n");
        printf("\ttoall;内容\t向所有在线用户发送。\r\n");
        printf("\tto:昵称;内容\t特定发送。\r\n");
        printf("\tgetusers;\t获取在线用户。\r\n");
        printf("\texit;\t退出。\r\n");
            continue;
        }
        else{
        webSocket_send(fd, buff, strlen(buff), true, WDT_TXTDATA);
        }
        }
    }
    printf("client close !\r\n");
    return 0;
}

